package com.keba.lamp.office.controller;

import cn.hutool.core.convert.Convert;
import com.keba.basic.annotation.log.WebLog;
import com.keba.basic.base.R;
import com.keba.basic.context.ContextConstants;
import com.keba.lamp.office.exception.ExceptionCodeEnum;
import com.keba.lamp.office.service.OfficeApiFacade;
import jakarta.servlet.http.HttpServletRequest;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;

@Slf4j
@RestController
@AllArgsConstructor
public class OfficeUploadController {

    private final OfficeApiFacade officeApiFacade;

    /**
     * 文件上传
     *
     * @param file_id
     * @return
     */
    @PutMapping("/anyTenant/v3/3rd/files/upload")
    @WebLog("office上传文件")
    public R<String> uploadFile(HttpServletRequest request) {
        //读取文件二进制流
        try {
            String fileId = request.getHeader("fileId");
            String fileName = request.getHeader("fileName");
            String fileSize = request.getHeader("fileSize");
            String userId = request.getHeader(ContextConstants.USER_ID_HEADER);
            log.info("==office上传文件:userId:{},fileId:{},文件名:{},文件大小:{}", userId, fileId, fileName, fileSize);
            officeApiFacade.uploadOffice(request.getInputStream(), fileId, fileName, Convert.toInt(fileSize), userId);
        } catch (IOException e) {
            return R.result(ExceptionCodeEnum.REQUEST_PARAMETER_ERROR.getCode(), "", ExceptionCodeEnum.REQUEST_PARAMETER_ERROR.getMsg());
        }
        return R.result(200, "", "上传成功");
    }

}
